package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MainActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.bean.BathRoomSkuBean;
import com.wnhz.work.yz.bean.OrderInfo;
import com.wnhz.work.yz.molder.EntiretyGoodsMolder2;
import com.wnhz.work.yz.molder.EntiretyMolder1;
import com.wnhz.work.yz.molder.EntiretyShoppingMolder;
import com.wnhz.work.yz.molder.EntiretyShoppingMolder2;
import com.wnhz.work.yz.utils.MyUtils;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 下单
 */
public class AddCart3Activity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "AddCart2Activity";
    private TextView shopping_number;
    private ImageView reduce;
    private EditText room_ed;
    private EditText remark_ed;
    private String money;
    private String packageId;
    private String skuId;
    private String nums;
    private DecimalFormat df;
    private String type;

    private TextView tv_all_price;
    private RelativeLayout btn_main_right;
    private List<BathRoomSkuBean.InfoEntity> bathRoomList = new ArrayList<>();
    private List<EntiretyShoppingMolder2.Info> skuJsonList = new ArrayList<>();
    private EntiretyShoppingMolder shoppingMolder;
    private EntiretyShoppingMolder shoppingMolder2;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_cart3);
        initTitle();
        money = getIntent().getStringExtra("money");
        packageId = getIntent().getStringExtra("packageId");
        skuId = getIntent().getStringExtra("skuId");
        type = getIntent().getStringExtra("type");
        bathRoomList = (List<BathRoomSkuBean.InfoEntity>) getIntent().getSerializableExtra("data");
        btn_main_right = (RelativeLayout) findViewById(R.id.btn_main_right);
        btn_main_right.setVisibility(View.GONE);
        room_ed = (EditText) findViewById(R.id.room_ed);
        remark_ed = (EditText) findViewById(R.id.remark_ed);
        tv_all_price = (TextView) findViewById(R.id.tv_all_price);
        reduce = (ImageView) findViewById(R.id.reduce);

        reduce.setOnClickListener(this);
        findViewById(R.id.ll_add).setOnClickListener(this);
        findViewById(R.id.tv_add_card).setOnClickListener(this);
        df = new DecimalFormat("######0.00");
        tv_all_price.setText("¥ " + df.format(Double.parseDouble(money)));
        shopping_number = (TextView) findViewById(R.id.shopping_number);

        if (null != getIntent()){
            shoppingMolder2 = (EntiretyShoppingMolder) getIntent().getSerializableExtra("molder");
            shoppingMolder = shoppingMolder2;
        }
    }

    private TextView title;

    private void initTitle() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("结算");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
    }

    private int num;

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
            case R.id.reduce://减少
                if (num > 1) {
                    num--;
                }
                if (num > 1) {
                    reduce.setImageResource(R.mipmap.ic_decrease_li);
                } else {
                    reduce.setImageResource(R.mipmap.ic_decrease_nor);
                }
                shopping_number.setText(num + "");
                tv_all_price.setText("￥ " + df.format(Double.parseDouble(money) * num));
                break;

            case R.id.ll_add://增加
                num++;
                shopping_number.setText(num + "");
                tv_all_price.setText("￥ " + df.format(Double.parseDouble(money) * num));
                break;
            //立即下单
            case R.id.tv_add_card:

                if (MyUtils.isFastClick()) {
                    //房间号
                    String room_number = room_ed.getText().toString();
                    //备注（选填）
                    String intro = remark_ed.getText().toString();
                    String package_id = packageId;
                    String color_id = "0";
                    String total_price = money;
                    shoppingMolder.setRoom_number(room_number);
                    shoppingMolder.setIntro(intro);
                    shoppingMolder.setPackage_id(package_id);
                    shoppingMolder.setColor_id(color_id);
                    shoppingMolder.setTotal_price(total_price);

                    showDialog();
                    String js = JSON.toJSONString(shoppingMolder);
                    System.out.println("js==="+js);


                    if (null != bathRoomList){
                        for (int i = 0; i <bathRoomList.size() ; i++) {
                            EntiretyShoppingMolder2.Info info = new EntiretyShoppingMolder2.Info();
                            info.setNums(bathRoomList.get(i).getChooseNum());
                            info.setPackage_id(bathRoomList.get(i).getPackage_id());
                            info.setSku_id(bathRoomList.get(i).getSku_id());
                            skuJsonList.add(info);
                        }
                    }

                    shoppingMolder2.setIntro(intro);
                    shoppingMolder2.setRoom_number(room_number + "");
                    shoppingMolder2.setNums(nums);

                    JSONArray jsonArray = new JSONArray();
                    JSONObject jsonObject = new JSONObject();
                    JSONObject tmpObj = null;
                    int count = skuJsonList.size();
                    for (int i = 0; i <count ; i++) {
                        tmpObj = new JSONObject();
                        try {
                            tmpObj.put("package_id",skuJsonList.get(i).getPackage_id());
                            tmpObj.put("sku_id",skuJsonList.get(i).getSku_id());
                            tmpObj.put("nums",skuJsonList.get(i).getNums());
                            jsonArray.put(tmpObj);
                            tmpObj = null;
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                    String js2 = jsonArray.toString();
                    System.out.println("js222==="+js2);
                    Map<String, Object> map = new HashMap<>();
                    map.put("token", MyApplication.getInstance().getUserBean().getToken());
                    map.put("package_id", packageId);
                    map.put("sku_json_str",js2);
                    map.put("json_str",js);
                    if(!intro.equals("")){
                        map.put("remark", intro);
                    }
                    map.put("type",type);

                    Set<String> set = map.keySet();
                    for (String s : set) {
                        Log.d(TAG, s+" : "+map.get(s));
                    }

                    XUtil.Post(Url.PACKAGE_ADD_ORDER, map, new MyCallBack<String>() {
                        @Override
                        public void onSuccess(String result) {
                            super.onSuccess(result);
                            Log.e("== 立即下单 ===", result);
                            try {
                                JSONObject jsonObject = new JSONObject(result);

                                String res = jsonObject.optString("result");
                                String info = jsonObject.optString("info");
                                if ("1".equals(res)) {
                                    MyToast(info);
                                    Intent intent1 = new Intent(AddCart3Activity.this, BathroomDetailActivity.class);
                                    startActivity(intent1);
                                    finish();
                                //    Gson gson = new Gson();
                                 //   OrderInfo.InfoBean infoBean = gson.fromJson(info, OrderInfo.InfoBean.class);

                                    //添加购物车立即下单
                               //     startActivity(SettleAccounts2Activity.createIntent(AddCart3Activity.this, infoBean.getPackage_id(), infoBean.getSku_id(), infoBean.getNums(), infoBean.getPackage_name(), infoBean.getPackage_sku_info(), infoBean.getImg(),infoBean.getRemark(), infoBean.getPackage_money(), infoBean.getType(), infoBean.getWeight(), infoBean.getPackage_cate(), infoBean.getInstall_fee()));
                               //    finish();
                                } else {

                                    MyToast(info);
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onFinished() {
                            closeDialog();
                            super.onFinished();
                        }

                        @Override
                        public void onError(Throwable ex, boolean isOnCallback) {
                            super.onError(ex, isOnCallback);
                            closeDialog();
                        }
                    });
                }

                break;
        }
    }
}
